package com.hmkj.core.mapper.amount;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.amount.RechargeModel;
import com.hmkj.core.po.amount.Recharge;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 充值记录相关Dao接口<br>
 *
 * @author donfy
 * @date 2017-08-15 08:16:12
 */
public interface RechargeMapper extends GenericMapper<Recharge, Integer> {

    /**
     * 通过sql统计
     * @param params 参数集合
     * @return List
     */
    @Override
    @Select("SELECT count(1) FROM fm_recharge t LEFT JOIN biz_user u ON t.user_id = u.id WHERE 1=1 ${whereSql} ${sortSql} ")
    long countBySqlJoin(Map<String, Object> params);

    /**
     * 通过sql查询
     * @param params 参数集合
     * @return List
     */
    @Override
    @Select("SELECT t.*,u.user_name AS 'u.userName',u.mobile AS 'u.mobile',u.real_name AS 'u.realName',u.mobile AS 'u.mobile',u.alipay_account AS 'u.alipayAccount' FROM fm_recharge t LEFT JOIN biz_user u ON t.user_id = u.id WHERE 1=1 ${whereSql} order by field(t.`status`,2,1,3,4) limit ${nowPage}, ${pageSize} ")
    List<RechargeModel> findListBySqlJoin(Map<String, Object> params);

    /**
     * 根据订单号 查询充值记录
     *
     * @param orderNo 订单号
     * @return Recharge
     */
    @Select("SELECT * FROM fm_recharge WHERE 1=1 and order_no = #{orderNo} and del_flag = 1")
    Recharge selectByOrderNo(@Param(value = "orderNo") String orderNo);

    /**
     * 前台充值列表
     *
     * @param param 参数集合
     * @return List<Recharge>
     */
    @Select(" <script> SELECT t.* " +
            "FROM fm_recharge t " +
            "WHERE t.user_id = #{userId} AND t.del_flag = 1 " +
            "<if test=\"type != null and type != ''\"> AND t.`type` = #{type} </if>" +
            "ORDER BY t.create_time DESC </script>")
    List<Recharge> findListForRechargeList(Map<String, Object> param);

    /**
     * 前台充值列表
     *
     * @param param 参数集合
     * @return Integer
     */
    @Select(" <script> SELECT count(*) " +
            "FROM fm_recharge t " +
            "WHERE t.user_id = #{userId} AND t.del_flag = 1 " +
            "<if test=\"type != null and type != ''\"> AND t.`type` = #{type} </if>" +
            "ORDER BY t.create_time DESC </script>")
    Integer findCountForRechargeList(Map<String, Object> param);

    /**
     * 获取充值总额
     * @param userId 用户id
     * @return BigDecimal
     */
    @Select("SELECT IFNULL(SUM(t.actual_arrival),0.00) AS 'rechargeMoney' FROM fm_recharge t WHERE t.user_id = #{userId} AND t.status = 3 AND t.del_flag = 1")
    BigDecimal findRechargeAllMoney(Integer userId);

    /**
     * 查询过期的未处理充值信息
     *
     * @param expiredTime 过期时间
     * @return List<Recharge>
     */
    @Select("SELECT t.* FROM fm_recharge t WHERE t.del_flag = 1 AND t.status = 1 AND t.create_time <= #{expiredTime} ORDER BY t.create_time ASC")
    List<Recharge> selectExpired(@Param(value = "expiredTime") Date expiredTime);

    /**
     * 根据type统计充值金额
     * @param params 参数
     * @return BigDecimal
     */
    @Select("SELECT SUM(t.amount) FROM fm_recharge t LEFT JOIN biz_user user ON user.id = t.user_id WHERE 1= 1 ${whereSql}")
    BigDecimal selectSumAmountByType(Map<String, Object> params);


    @Select("SELECT COUNT(1) AS rechargeNum, SUM(t1.amo)AS totalAmo FROM(SELECT SUM(1) AS num, SUM(t.amount) AS amo FROM fm_recharge t WHERE 1= 1 ${whereSql}) t1")
    RechargeModel selectNumByTime(Map<String, Object> params);

    /**
     * 通过用户id和订单号查询充值订单
     * @param orderId   订单号
     * @return          订单
     */
    @Select("SELECT t.* FROM fm_recharge t WHERE t.del_flag = 1  AND t.order_no = #{orderId}")
    Recharge findByUserIdAndOrderNo(String orderId);

}
